package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //  套餐分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        //1.设置分页参数,通过PageHelper.startPage()方法
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //2.调用mapper进行查询 , 强转为Page
        Page<SetmealVO> page = setmealMapper.pageQuery(dto);
        //3.封装PageResult对象并返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    //  新增套餐
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO dto) {
        //构建套餐对象,  将dto对象属性复制到套餐对象中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //  插入套餐基本数据
        setmealMapper.insert(setmeal);
        //添加套餐对应的菜品数据
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //  关联套餐id
        setmealDishes.forEach(
                setmealDish -> setmealDish.setSetmealId(setmeal.getId())
        );
        //  批量插入
        setmealDishMapper.insertBatch(setmealDishes);
    }

    //  回显套餐
    @Override
    public SetmealVO getById(Long id) {
     //创建SetmealVO对象用来封装数据给前页面
        SetmealVO setmealVO = new SetmealVO();
        //1.根据id查询套餐数据,并赋值给dishVO对象
        Setmeal setmeal = setmealMapper.getById(id);
        //将查询到内容赋给dishVO对象
        BeanUtils.copyProperties(setmeal,setmealVO);
        //2.根据套餐id查询对应套餐的菜品数据,并赋值给dishVO对象
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    //  修改套餐
    @Transactional
    @Override
    public void update(SetmealDTO dto) {
        //构建套餐对象,  将dto对象属性复制到套餐对象中
        Setmeal setmeal = new Setmeal();
        //  将dto对象属性复制到套餐对象中
        BeanUtils.copyProperties(dto,setmeal);
        //  修改套餐基本数据
        setmealMapper.update(setmeal);
        //修改套餐对应的菜品数据
        //  删除当前套餐对应的菜品数据
        setmealDishMapper.deleteBySetmealId(dto.getId());
        //  添加新的菜品数据
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //进行判空,在菜品数据不等于空时才进行添加
        if (setmealDishes != null && setmealDishes.size() > 0) {
            //  关联套餐id
            setmealDishes.forEach(
                    setmealDish -> setmealDish.setSetmealId(setmeal.getId())
            );
            //  批量插入
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    //  停售起售
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    //  批量删除
    @Override
    public void delete(List<Long> ids) {
        //1.删除套餐之前，需要判断套餐是否启售，启售中不允许删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE ){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //2.删除套餐基本信息
        setmealMapper.deleteBatch(ids);
        //3.删除套餐关联的菜品信息
        setmealDishMapper.deleteSetmealDishBySetmealId(ids);
    }

}
